package com.java并发编程.semaphore;

import java.util.concurrent.Semaphore;

public class SemaphoreTest1 {
	
	Semaphore s = new Semaphore(1);
	
	public void print() throws Exception {
		s.acquire();
		System.out.println(Thread.currentThread().getName() + " :begin...");
		Thread.sleep(1000);
		s.release();
		System.out.println(Thread.currentThread().getName() + " :end...");
	}
	
	
	
	public static void main(String[] args) {
		final SemaphoreTest1 sTest1 = new SemaphoreTest1();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					sTest1.print();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}, "A").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					sTest1.print();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}, "B").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					sTest1.print();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}, "C").start();
	}

}
